package com.mini.gateway.core.modules.apis.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ApiService
 *
 */
@Slf4j
@Component
public class ApiService {

    @Autowired
    private RestTemplate restTemplate;

    public HashMap<String, Object> getPath(String serviceId) {
        List<String> resultList = new ArrayList<>();
        String uri = "http://localhost:19200/v2/api-docs";

        HashMap<String, Object> map = restTemplate.getForObject(uri, HashMap.class);

        JSONArray tagArray = (JSONArray) map.get("tags");
        for (Object object : tagArray) {
            JSONObject _obj = (JSONObject) object;
            System.out.println(_obj.get("name") + " --> " + _obj.get("description"));
        }
        JSONObject pathObject = (JSONObject) map.get("paths");
        for (String key : pathObject.keySet()) {
            System.out.println(key);
        }
        resultList.addAll(map.keySet());
        return map;
    }

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
        restTemplate.getMessageConverters().add(0, new FastJsonHttpMessageConverter());

        String uri = "http://192.168.11.15:19100/v2/api-docs";

        JSONObject map = restTemplate.getForObject(uri, JSONObject.class);

        JSONArray tagArray = map.getJSONArray("tags");
        for (Object object : tagArray) {
            JSONObject _obj = (JSONObject) object;
            System.out.println(_obj.get("name") + " --> " + _obj.get("description"));
        }
        JSONObject pathObject = map.getJSONObject("paths");
        JSONObject detailObj;
        for (String key : pathObject.keySet()) {
            System.out.println(key);
            JSONObject methodObj = pathObject.getJSONObject(key);
            detailObj = methodObj.getJSONObject("get");

            if (detailObj == null) {
                detailObj = methodObj.getJSONObject("put");

            }
            if (detailObj == null) {
                detailObj = methodObj.getJSONObject("post");

            }
            if (detailObj == null) {
                detailObj = methodObj.getJSONObject("delete");

            }
            System.out.println(detailObj);
        }
    }

    private static ClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(30000);
        factory.setConnectTimeout(30000);
        return factory;
    }
}
